//
//  YHPhotoManager.m
//  ScriptKill
//
//  Created by Mac on 2021/4/25.
//  Copyright © 2021 优智汇. All rights reserved.
//

#import "YHPhotoManager.h"
#import <HXPhotoPicker/UIViewController+HXExtension.h>
#import "YHDefines.h"


UIViewController * YHPhotoFilterVCLoop(UIViewController * vc){
    UIViewController *result = vc;
    while (result.presentedViewController) {
        result = result.presentedViewController;
    }
    if ([result isKindOfClass:[UITabBarController class]]) {
        result = [(UITabBarController *)result selectedViewController];
    }
    if ([result isKindOfClass:[UINavigationController class]]) {
        result = [(UINavigationController *)result topViewController];
    }
    if ([result isKindOfClass:[UITabBarController class]]) {
        return YHPhotoFilterVCLoop(result);
    }
    if ([result isKindOfClass:[UINavigationController class]]) {
        return YHPhotoFilterVCLoop(result);
    }
    return result;
}


UIViewController * YHPhotoCurrentViewController(void){
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal){
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows){
            if (tmpWin.windowLevel == UIWindowLevelNormal){
                window = tmpWin;
                break;
            }
        }
    }
    UIViewController *result = YHPhotoFilterVCLoop(window.rootViewController);
    
    return result;
}


@interface YHPhotoManager ()


@end


@implementation YHPhotoManager


-(void)showSelectPhoto:(void (^)(NSArray<HXPhotoModel *> * _Nonnull, YHPhotoManager * _Nonnull))selectBlock{
    
    [self showSelect:selectBlock];

}

-(void)showSelectPhotoAndCamera:(void (^)(NSArray<HXPhotoModel *> * _Nonnull, YHPhotoManager * _Nonnull))selectBlock{
    self.manager.configuration.openCamera = YES;
    [self showSelect:selectBlock];
}

-(void)showSelect:(void (^)(NSArray<HXPhotoModel *> * _Nonnull,YHPhotoManager * _Nonnull))selectBlock{
    WS(weakSelf)
    [YHPhotoCurrentViewController() hx_presentSelectPhotoControllerWithManager:self.manager didDone:^(NSArray<HXPhotoModel *> * _Nullable allList, NSArray<HXPhotoModel *> * _Nullable photoList, NSArray<HXPhotoModel *> * _Nullable videoList, BOOL isOriginal, UIViewController * _Nullable viewController, HXPhotoManager * _Nullable manager) {
            if (selectBlock) {
                selectBlock(photoList,weakSelf);
            }
        } cancel:^(UIViewController * _Nullable viewController, HXPhotoManager * _Nullable manager) {
            
        }];
}


- (HXPhotoManager *)manager {
    if(_manager){
        return _manager;
    }
    HXPhotoManager * manager = [[HXPhotoManager alloc] initWithType:HXPhotoManagerSelectedTypePhoto];
    manager.videoSelectedType = HXPhotoManagerVideoSelectedTypeSingle;
    manager.configuration.photoMaxNum = 1;
    manager.configuration.videoMaxNum = 0;
    manager.configuration.selectTogether = NO;
    manager.configuration.maxNum = 1;
    manager.configuration.cameraCellShowPreview = NO;
    manager.configuration.openCamera =NO;
    manager.configuration.photoCanEdit =NO;
    manager.configuration.showBottomPhotoDetail = NO;
    manager.configuration.saveSystemAblum = YES;
    manager.configuration.photoStyle = HXPhotoStyleDefault;
    manager.configuration.singleJumpEdit = YES;
    manager.configuration.photoCanEdit = YES;
    manager.configuration.editAssetSaveSystemAblum = YES;
    manager.configuration.saveSystemAblum = YES;
    _manager = manager;
    return _manager;
}


@end
